cmake_minimum_required(VERSION 3.16)
project(LabUI VERSION 1.0 LANGUAGES C CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Set up AUTOMOC and some sensible defaults for runtime execution
# When using Qt 6.3, you can replace the code block below with
# qt_standard_project_setup()
set(CMAKE_AUTOMOC ON)
include(GNUInstallDirs)
set(CMAKE_AUTOUIC ON)

find_package(QT NAMES Qt5 Qt6 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Gui Quick QuickWidgets)
find_package(Qt${QT_VERSION_MAJOR} OPTIONAL_COMPONENTS Widgets)

message(STATUS "QQQQQQQQQQQQ Qt5_DIR: ${Qt5_DIR}")

add_executable(LabUI WIN32 MACOSX_BUNDLE
    MainWindow.cpp MainWindow.h MainWindow.ui
    Miscellaneous.cpp Miscellaneous.h
    ThreadDemo.cpp ThreadDemo.h
    View/FirstQuickView.cpp View/FirstQuickView.h
    WidgetFactory.cpp WidgetFactory.h WidgetFactory.ui
    main.cpp
)
target_include_directories(LabUI PRIVATE
    ../thirdparty/sentry/win32/include
)

target_compile_definitions(LabUI PRIVATE
    QT_DEPRECATED_WARNINGS
)

target_link_libraries(LabUI PRIVATE

    # Remove: L../DynLibExp
    # Remove: L../DynLibExp/debug
    # Remove: L../DynLibExp/release
    # Remove: L../StaticLibExp/debug
    # Remove: L../StaticLibExp/release
    Qt::Core
    Qt::Gui
    Qt::Quick
    Qt::QuickWidgets

    # sentry
)

if((QT_VERSION_MAJOR GREATER 4))
    target_link_libraries(LabUI PRIVATE
        Qt::Widgets
    )
endif()

if(APPLE)
    target_link_libraries(LabUI PRIVATE

        # Remove: L${CMAKE_CURRENT_SOURCE_DIR}/../DynLibExp
        # Remove: L${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/sentry/mac
        ../DynLibExp/libDynLibExp.dylib
        ../StaticLibExp/build//libStaticLibExp.a
    )
endif()

if(UNIX)
    target_link_libraries(LabUI PRIVATE
        ../StaticLibExp/build//libStaticLibExp.a
    )
endif()

if(WIN32)
    target_link_libraries(LabUI PRIVATE

        # Remove: L${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/sentry/win32
        ../Resources/Debug/Resources
        ../StaticLibExp/Debug/StaticLibExp
        DynLibExp
        iphlpapi
        ws2_32
    )

    # add_custom_command(TARGET LabUI POST_BUILD
    # COMMAND ${CMAKE_COMMAND} -E copy_if_different
    # ${CMAKE_SOURCE_DIR}/../thirdparty/sentry/win32/sentry.dll
    # $<TARGET_FILE_DIR:LabUI>)

    # add_custom_command(TARGET LabUI POST_BUILD
    # COMMAND ${CMAKE_COMMAND} -E copy_if_different
    # ${CMAKE_SOURCE_DIR}/../thirdparty/sentry/win32/sentry.lib
    # $<TARGET_FILE_DIR:LabUI>)
    message("QQQQQQ CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
    message("QQQQQQ TARGET_FILE_DIR: $<TARGET_FILE_DIR:LabUI>")
    message("QQQQQQQQQQ TARGET_FILE_DIR: ${CMAKE_SOURCE_DIR}")
    add_custom_command(TARGET LabUI PRE_LINK
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_SOURCE_DIR}/thirdparty/sentry/win32/sentry.dll
        $<TARGET_FILE_DIR:LabUI>
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_SOURCE_DIR}/thirdparty/sentry/win32/sentry.lib
        $<TARGET_FILE_DIR:LabUI>
        COMMAND ${CMAKE_COMMAND} -E echo "QQQQQQQQ Copying sentry.dll..."
    )
endif()

# copy dependent dll
add_custom_command(TARGET LabUI POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    $<TARGET_FILE:DynLibExp>
    $<TARGET_FILE_DIR:LabUI>
    COMMENT "Copying DynLibExp.dll...")

# deployqt
find_program(WINDEPLOYQT_EXECUTABLE NAMES windeployqt HINTS ${QTDIR} ENV QTDIR PATH_SUFFIXES bin)
add_custom_command(TARGET LabUI POST_BUILD
    COMMAND ${WINDEPLOYQT_EXECUTABLE} $<TARGET_FILE:LabUI>
    COMMAND ${CMAKE_COMMAND} -E echo "QQQQQQQQ windeployqt ${WINDEPLOYQT_EXECUTABLE}"
    COMMENT "Running windeployqt...")

install(TARGETS LabUI
    BUNDLE DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# Consider using qt_generate_deploy_app_script() for app deployment if
# the project can use Qt 6.3. In that case rerun qmake2cmake with
# --min-qt-version=6.3.
